#include <bits/stdc++.h>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution
{
public:
    pair<TreeNode *, int> solve(TreeNode *root)
    {
        if (!root)
        {
            return {root, 0};
        }
        pair<TreeNode *, int> left = solve(root->left);
        pair<TreeNode *, int> right = solve(root->right);
        if (left.second > right.second)
        {
            return {left.first, left.second + 1};
        }
        else if (left.second < right.second)
        {
            return {right.first, right.second + 1};
        }
        return {root, left.second + 1};
    }
    TreeNode *lcaDeepestLeaves(TreeNode *root)
    {
        return solve(root).first;
    }
};